
/* 换床  */
function showLiveMember(className){
	showPop(className)

	var currentdate = getNowFormatDate();
	$("#liveMemberForm input[name=startDate]").val(currentdate);
									
}
// 获取当前时间——格式：2012-12-06
function getNowFormatDate() {
    var date = new Date();
    var seperator = "-";
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (day >= 0 && day <= 9) {
    	day = "0" + day;
    }
    var currentdate = date.getFullYear() + seperator + month + seperator + day;
    return currentdate;
}

/* 弹窗 */
//显示弹框
function showPop(className){
	$(".zhy_zhezhao_pop").show();
	var top = document.body.scrollTop||document.documentElement.scrollTop;
	var this_h = $("." + className).height();
	var win_h = ($('html',parent.document).height()-this_h-100)/2
	$(".zhy_zhezhao_pop").height($("html").height());
	var this_h = $("." + className).height();
	top= top +win_h;
	$("." + className).css("top", top).show().siblings(".zhy_popa").hide();
}

// 隐藏修改弹框
function hidePop(className) {    
	$(".zhy_zhezhao_pop").hide();
	$(".zhy_popa").hide();
	$("."+className).empty();
}

/* 计算费用 */
// 计算床位费用
var payBedCost;
var payBedPreCost;
var payOtherCost;
function calculateBedCost(currBedPrice,accountRemain,bedCostRemain,oldBedPrice){
	currBedPrice = parseFloat(currBedPrice);
	bedCostRemain = parseFloat(bedCostRemain);
	oldBedPrice = parseFloat(oldBedPrice);
	
	var startDate = $("#liveMemberForm input[name=startDate]").val();
	var endDate = $("#liveMemberForm input[name=endDate]").val();			
       
	if (startDate != "" && endDate != ""){
		var dateDiff = (Date.parse(endDate.replace(/-/g, "/")) - Date.parse(startDate.replace(/-/g, "/")))/86400000;
		var newBedCost = currBedPrice * dateDiff;

		if (dateDiff <= 0) {
			zhyAlert("换床开始日期不能大于等于结束日期");
		    return false;
		}
		
		if (newBedCost >= 0 && dateDiff > 0){
			$("#liveMemberForm #bedCost").text(newBedCost + "元");
			$("#liveMemberForm input[name=bedCost]").val(newBedCost);
			
			// 床位费用
			var currentdate = getNowFormatDate();
			var oldDateDiff = (Date.parse(startDate.replace(/-/g, "/")) - Date.parse(currentdate.replace(/-/g, "/")))/86400000;
			var realBedCostRemain = bedCostRemain - oldBedPrice * oldDateDiff;
			payBedCost = newBedCost - realBedCostRemain;
			if (payBedCost >= 0){
				$("#payBedCostDisplay").css("display", "block")
				$("#payBedCost").text(payBedCost);
				
				$("#backBedCostDisplay").css("display", "none")
				$("#backBedCost").text("");						
			}else{
				$("#payBedCostDisplay").css("display", "none")
				$("#payBedCost").text("");
				
				$("#backBedCostDisplay").css("display", "block")
				$("#backBedCost").text(-payBedCost);
			}		
		}else{
			$("#liveMemberForm #bedCost").text("");
			$("#liveMemberForm input[name=bedCost]").val("");
		}
	}

	changeTotalCost(accountRemain);
}
// 计算预付押金
function calculatePreCost(oldBedPreCost,accountRemain){
	var newBedPreCost = $("#liveMemberForm input[name=bedPreCost]").val();

	if (newBedPreCost != null && newBedPreCost != ""){
		payBedPreCost = parseFloat(newBedPreCost) - parseFloat(oldBedPreCost);
		if (payBedPreCost >= 0){
			$("#payBedPreCostDisplay").css("display", "block")
			$("#payBedPreCost").text(payBedPreCost);

			$("#backBedPreCostDisplay").css("display", "none")
			$("#backBedPreCost").text("");		
		}else{
			$("#payBedPreCostDisplay").css("display", "none")
			$("#payBedPreCost").text("");
			
			$("#backBedPreCostDisplay").css("display", "block")
			$("#backBedPreCost").text(-payBedPreCost);
		}
	}else{
		$("#payBedPreCostDisplay").css("display", "none")
		$("#payBedPreCost").text("");

		$("#backBedPreCostDisplay").css("display", "none")
		$("#backBedPreCost").text("");
	}
	
	changeTotalCost(accountRemain);
}
// 计算其它费用
function calculateOtherCost(oldOtherCost,accountRemain){
	var newOtherCost = $("input[name=otherCost]").val();

	if (newOtherCost != null && newOtherCost != ""){
		payOtherCost = parseFloat(newOtherCost) - parseFloat(oldOtherCost);
		if (payOtherCost >= 0){
			$("#payOtherCostDisplay").css("display", "block")
			$("#payOtherCost").text(payOtherCost);

			$("#backOtherCostDisplay").css("display", "none")
			$("#backOtherCost").text("");
		}else{
			$("#backOtherCostDisplay").css("display", "block")
			$("#backOtherCost").text(-payOtherCost);

			$("#payOtherCostDisplay").css("display", "none")
			$("#payOtherCost").text("");
		}
	}else {
		$("#payOtherCostDisplay").css("display", "none")
		$("#payOtherCost").text("");

		$("#backOtherCostDisplay").css("display", "none")
		$("#backOtherCost").text("");
	}
	
	changeTotalCost(accountRemain);
}
// 总费用变更处理
function changeTotalCost(accountRemain){
	if (payBedCost != null && payBedPreCost != null && payOtherCost != null){
		var payTotalCost = payBedCost + payBedPreCost + payOtherCost - parseFloat(accountRemain);
		
		if (payTotalCost >= 0){
			$("#payTotalCostDisplay").css("display", "block")
			$("#payTotalCost").text(payTotalCost);		
			$("input[name=payTotalCost").val(payTotalCost);		

			$("#backTotalCostDisplay").css("display", "none")
			$("#backTotalCost").text("");
			$("input[name=backTotalCost").val("");
			
			$("#realPayCostDisplay").css("display", "block")	
		}else{
			$("#backTotalCostDisplay").css("display", "block")
			$("#backTotalCost").text(-payTotalCost);
			$("input[name=backTotalCost").val(-payTotalCost);

			$("#payTotalCostDisplay").css("display", "none")
			$("#payTotalCost").text("");
			$("input[name=payTotalCost").val("");

			$("#realPayCostDisplay").css("display", "none")	
			$("#oweReasonDisplay").css("display", "none");		
		}
	}else{
		$("#payTotalCostDisplay").css("display", "none")
		$("#payTotalCost").text("");
		$("input[name=payTotalCost").val("");

		$("#backTotalCostDisplay").css("display", "none")
		$("#backTotalCost").text("");
		$("input[name=backTotalCost").val("");
		
		$("#realPayCostDisplay").css("display", "none")	
		$("#oweReasonDisplay").css("display", "none");		
	}
}

// 实缴费用-是否显示欠费原因
function realPayTotalCost(){
	var payCost = $("#payTotalCost").text();
	var realPayCost = $("input[name=realPayCost]").val();

	if (parseFloat(realPayCost) < parseFloat(payCost)){
		$("#oweReasonDisplay").css("display", "block");
	}else{
		$("#oweReasonDisplay").css("display", "none");	
	}
}

// 检查是否为空
function checkIsEmpty(obj, errorId) {
	var value = obj.val();
	if (null == value || value == ""){
		$("#liveMemberForm #"+errorId).css("display", "block");
		return false;
	}else{
		$("#liveMemberForm #"+errorId).css("display", "none");
		return true;
	}
}

//校验换床表单
function liveMemberFormSubmit(accountRemain){
	if(!$("#liveMemberForm").validationEngine("validate")){
		return false;
	} 
	
	var startDate = $("input[name='startDate']").val();
	var endDate = $("input[name='endDate']").val();
	if (startDate > endDate) {
		zhyAlert("换床开始日期不能大于等于结束日期");
	    return false;
	}

	var realPayCost = $("input[name=realPayCost]").val();
	var payCost = parseFloat(realPayCost) + parseFloat(accountRemain);
	var payCostMust = parseFloat(payBedPreCost) + parseFloat(payOtherCost);
	if (payCost < payCostMust){
		zhyAlert("请交齐入住押金和其它费用")
		return false;
	}
	
	$("#liveMemberForm").submit();
}
